﻿using System;
using System.Collections;
using System.Text;
using smartBrowser.Util;
using System.Threading;
using smartBrowser.GUI;
using SemWeb;

namespace smartBrowser.Task
{
    /// <summary>
    /// @author Joel
    /// </summary>  
    public class WordnetTask: Task
    {
        private const String Url = "http://xmlns.com/wordnet/1.6/";
        private Hashtable ontologyList;

        public WordnetTask(WebSite webSite, TabPageForm form)
            : base(webSite, form)
        {
            this.ontologyList = new Hashtable();
        }
        
        protected override void Execute()
        {
            Console.Out.WriteLine("Wordnet TASK START");

            foreach (Tag tag in webSite.TopKeywords)
            {                                
                String concept = tag.Concept;
                concept = concept.Trim();
                concept = concept.Replace(' ', '_');                
                MemoryStore store = this.load(new Uri(Url + concept));
                if (store!= null && store.StatementCount > 0)
                {
                    Console.Out.WriteLine("Wordnet devolveu-me uma ontologia para conceito: " + concept);
                    this.ontologyList.Add(concept, store);
                }                
            }
           
            FireCompleteEvent();
            Console.Out.WriteLine("Wordnet TASK COMPLETE");
        }

        private MemoryStore load(Uri uri)
        {
            MemoryStore store = new MemoryStore();
            String name = uri.AbsoluteUri;

            //Carrega o rdf do uri           
            try
            {
                RdfReader rdf = RdfXmlReader.LoadFromUri(uri);
                store.Import(rdf);
                rdf.Dispose();
                Console.Out.WriteLine("Ontology Loaded from URI.");
                return store;

            }
            catch (Exception exc)
            {
                Console.Out.WriteLine("Error loading the ontology from swoogle [" + uri.AbsoluteUri + "] : " + exc.Message);
            }

            return null;
        }

        public Hashtable OntologyList
        {
            get
            {
                return this.ontologyList;
            }
        }
    }
}
